<?php

class CalcOrderClass
{
	public $_explicitType = "presstrack.CalcOrderClass";
	
	public $arPriceCutting;
	public $arPriceSM74;
	public $arPriceSM52;	
	public $arPriceFoldingClip;
	public $arPricePerforation;
	
	function __construct() 
	{
		$this->arPriceCutting = array();
		$this->arPriceCutting["1"][]	= array('run'=>1000,	'cost'=>100);
		$this->arPriceCutting["1"][]	= array('run'=>2000,	'cost'=>140);
		$this->arPriceCutting["1"][]	= array('run'=>5000,	'cost'=>250);
		$this->arPriceCutting["1"][]	= array('run'=>10000,	'cost'=>400);
		
		$this->arPriceSM74	= array();
		$this->arPriceSM74["4"][]	= array('run'=>500,		'cost'=>120);
		$this->arPriceSM74["4"][]	= array('run'=>1000,	'cost'=>130);
		$this->arPriceSM74["4"][]	= array('run'=>2000,	'cost'=>140);
		$this->arPriceSM74["4"][]	= array('run'=>3000,	'cost'=>150);
		$this->arPriceSM74["4"][]	= array('run'=>5000,	'cost'=>170);
		$this->arPriceSM74["4"][]	= array('run'=>7000,	'cost'=>190);
		$this->arPriceSM74["4"][]	= array('run'=>10000,	'cost'=>220);
		$this->arPriceSM74["4"][]	= array('run'=>15000,	'cost'=>330);
		$this->arPriceSM74["4"][]	= array('run'=>20000,	'cost'=>400);
		$this->arPriceSM74["4"][]	= array('run'=>30000,	'cost'=>520);
		$this->arPriceSM74["4"][]	= array('run'=>50000,	'cost'=>880);
		
		$this->arPriceSM74["18"][]	= array('run'=>500,		'cost'=>240);
		$this->arPriceSM74["18"][]	= array('run'=>1000,	'cost'=>260);
		$this->arPriceSM74["18"][]	= array('run'=>2000,	'cost'=>280);
		$this->arPriceSM74["18"][]	= array('run'=>3000,	'cost'=>300);
		$this->arPriceSM74["18"][]	= array('run'=>5000,	'cost'=>340);
		$this->arPriceSM74["18"][]	= array('run'=>7000,	'cost'=>380);
		$this->arPriceSM74["18"][]	= array('run'=>10000,	'cost'=>440);
		$this->arPriceSM74["18"][]	= array('run'=>15000,	'cost'=>660);
		$this->arPriceSM74["18"][]	= array('run'=>20000,	'cost'=>800);
		$this->arPriceSM74["18"][]	= array('run'=>30000,	'cost'=>1040);
		$this->arPriceSM74["18"][]	= array('run'=>50000,	'cost'=>1760);
		
		$this->arPriceSM52	= array();
		$this->arPriceSM52["1"][]	= array('run'=>500,		'cost'=>16);
		$this->arPriceSM52["1"][]	= array('run'=>1000,	'cost'=>19);
		$this->arPriceSM52["1"][]	= array('run'=>2000,	'cost'=>25);
		$this->arPriceSM52["1"][]	= array('run'=>3000,	'cost'=>33);
		$this->arPriceSM52["1"][]	= array('run'=>5000,	'cost'=>42);
		$this->arPriceSM52["1"][]	= array('run'=>7000,	'cost'=>60);
		$this->arPriceSM52["1"][]	= array('run'=>10000,	'cost'=>75);
		$this->arPriceSM52["1"][]	= array('run'=>15000,	'cost'=>100);
		$this->arPriceSM52["1"][]	= array('run'=>20000,	'cost'=>125);
		$this->arPriceSM52["1"][]	= array('run'=>30000,	'cost'=>165);
		$this->arPriceSM52["1"][]	= array('run'=>50000,	'cost'=>220);
		
		$this->arPriceSM52["2"][]	= array('run'=>500,		'cost'=>32);
		$this->arPriceSM52["2"][]	= array('run'=>1000,	'cost'=>38);
		$this->arPriceSM52["2"][]	= array('run'=>2000,	'cost'=>50);
		$this->arPriceSM52["2"][]	= array('run'=>3000,	'cost'=>66);
		$this->arPriceSM52["2"][]	= array('run'=>5000,	'cost'=>84);
		$this->arPriceSM52["2"][]	= array('run'=>7000,	'cost'=>120);
		$this->arPriceSM52["2"][]	= array('run'=>10000,	'cost'=>150);
		$this->arPriceSM52["2"][]	= array('run'=>15000,	'cost'=>200);
		$this->arPriceSM52["2"][]	= array('run'=>20000,	'cost'=>250);
		$this->arPriceSM52["2"][]	= array('run'=>30000,	'cost'=>330);
		$this->arPriceSM52["2"][]	= array('run'=>50000,	'cost'=>440);
		
		$this->arPriceSM52["3"][]	= array('run'=>500,		'cost'=>48);
		$this->arPriceSM52["3"][]	= array('run'=>1000,	'cost'=>57);
		$this->arPriceSM52["3"][]	= array('run'=>2000,	'cost'=>75);
		$this->arPriceSM52["3"][]	= array('run'=>3000,	'cost'=>99);
		$this->arPriceSM52["3"][]	= array('run'=>5000,	'cost'=>124);
		$this->arPriceSM52["3"][]	= array('run'=>7000,	'cost'=>180);
		$this->arPriceSM52["3"][]	= array('run'=>10000,	'cost'=>225);
		$this->arPriceSM52["3"][]	= array('run'=>15000,	'cost'=>300);
		$this->arPriceSM52["3"][]	= array('run'=>20000,	'cost'=>375);
		$this->arPriceSM52["3"][]	= array('run'=>30000,	'cost'=>495);
		$this->arPriceSM52["3"][]	= array('run'=>50000,	'cost'=>660);
		
		$this->arPriceSM52["4"][]	= array('run'=>500,		'cost'=>60);
		$this->arPriceSM52["4"][]	= array('run'=>1000,	'cost'=>65);
		$this->arPriceSM52["4"][]	= array('run'=>2000,	'cost'=>85);
		$this->arPriceSM52["4"][]	= array('run'=>3000,	'cost'=>105);
		$this->arPriceSM52["4"][]	= array('run'=>5000,	'cost'=>140);
		$this->arPriceSM52["4"][]	= array('run'=>7000,	'cost'=>175);
		$this->arPriceSM52["4"][]	= array('run'=>10000,	'cost'=>220);
		$this->arPriceSM52["4"][]	= array('run'=>15000,	'cost'=>280);
		$this->arPriceSM52["4"][]	= array('run'=>20000,	'cost'=>330);
		$this->arPriceSM52["4"][]	= array('run'=>30000,	'cost'=>410);
		$this->arPriceSM52["4"][]	= array('run'=>50000,	'cost'=>540);
		
		$this->arPriceSM52["18"][]	= array('run'=>500,		'cost'=>120);
		$this->arPriceSM52["18"][]	= array('run'=>1000,	'cost'=>130);
		$this->arPriceSM52["18"][]	= array('run'=>2000,	'cost'=>170);
		$this->arPriceSM52["18"][]	= array('run'=>3000,	'cost'=>210);
		$this->arPriceSM52["18"][]	= array('run'=>5000,	'cost'=>280);
		$this->arPriceSM52["18"][]	= array('run'=>7000,	'cost'=>350);
		$this->arPriceSM52["18"][]	= array('run'=>10000,	'cost'=>440);
		$this->arPriceSM52["18"][]	= array('run'=>15000,	'cost'=>560);
		$this->arPriceSM52["18"][]	= array('run'=>20000,	'cost'=>660);
		$this->arPriceSM52["18"][]	= array('run'=>30000,	'cost'=>820);
		$this->arPriceSM52["18"][]	= array('run'=>50000,	'cost'=>1080);
		
		$this->arPriceFoldingClip = array();
		$this->arPriceFoldingClip["1"][]	= array('run'=>500,		'cost'=>1050);
		$this->arPriceFoldingClip["1"][]	= array('run'=>1000,	'cost'=>1350);
		$this->arPriceFoldingClip["1"][]	= array('run'=>2000,	'cost'=>1950);
		$this->arPriceFoldingClip["1"][]	= array('run'=>3000,	'cost'=>2700);
		$this->arPriceFoldingClip["1"][]	= array('run'=>5000,	'cost'=>4200);
		$this->arPriceFoldingClip["1"][]	= array('run'=>7000,	'cost'=>5700);
		$this->arPriceFoldingClip["1"][]	= array('run'=>10000,	'cost'=>7500);
		
		$this->arPriceFoldingTermo = array();
		$this->arPriceFoldingTermo["1"][]	= array('run'=>300,		'cost'=>7.5);
		$this->arPriceFoldingTermo["1"][]	= array('run'=>500,		'cost'=>7);
		$this->arPriceFoldingTermo["1"][]	= array('run'=>1000,	'cost'=>6);
		$this->arPriceFoldingTermo["1"][]	= array('run'=>3000,	'cost'=>5.4);
		$this->arPriceFoldingTermo["1"][]	= array('run'=>5000,	'cost'=>5.1);
		$this->arPriceFoldingTermo["1"][]	= array('run'=>10000,	'cost'=>4);
		
		$this->arPricePerforation = array();
		$this->arPricePerforation["1"][]	= array('run'=>5000,	'cost'=>0.15);
		$this->arPricePerforation["1"][]	= array('run'=>7000,	'cost'=>0.135);
		$this->arPricePerforation["1"][]	= array('run'=>10000,	'cost'=>0.114);
    }
	
	function setCostFor($task) 
	{
		if ($task->label		== "вывод форм") 
		{
			$this->CostCtp($task);
		}
		elseif ($task->label	== "резка") 
		{
			$this->CostCutting($task);
		}
		elseif ($task->label 	== "фальцовка") 
		{
			$this->CostFolding($task);
		}
		elseif ($task->label	== "печать")
		{
			$this->CostPress($task);
		}
		elseif ($task->label	== "скрепление")
		{
			$this->CostВinding($task);
		}
		elseif ($task->label	== "перфорация")
		{
			$this->CostPerforation($task);
		}
		elseif ($task->label == "цветопроба") 
		{
			$this->proofCost($task);
		}
		elseif ($task->label == "ламинация") 
		{
			$this->laminatorCost($task);
		}	
		elseif ($task->label == "вырубка") 
		{
			$this->stanzCost($task);
		}				
	}
	
	/**
	 * CostCtp
	 *	 
     * рассчет стоимости вывода форм 
	 *
	 * :WARNING: 
	 *
	 * стоимость одной пластины формата А2 - 350 руб.
	 * стоимость одной пластины формата А3 - 175 руб.
	 *
	 * @param	object	$task	задание на вывод форм
     */
	function CostCtp($task) 
	{
		$ctp_temp = explode("\r", $task->value);
		$form_num = trim(str_replace("форм : ", "", $ctp_temp[0]));
		
		$sql = "SELECT product_id AS product_id FROM press_track_new.relation WHERE task_id = $task->idtask LIMIT 1";
		$query = mysql_query($sql);
		$pr = mysql_fetch_object($query);
		
		$sql = "SELECT value AS value FROM press_track_new.task WHERE press_track_new.task.label = 'печать' 
		AND press_track_new.task.idtask IN ( SELECT task_id FROM press_track_new.relation WHERE product_id = $pr->product_id )";
		$query = mysql_query($sql);
		$press_task = mysql_fetch_object($query);
		$press_temp = explode("\r", $press_task->value);
		$press_label = trim(str_replace("машина : ", "", $press_temp[0]));		
		
		if ($press_label == "SM-74") 
		{
			$sql = "UPDATE task SET cost = (350 * $form_num) WHERE idtask = $task->idtask";
		}
		elseif ($press_label == "SM-52") 
		{
			$sql = "UPDATE task SET cost = (175 * $form_num) WHERE idtask = $task->idtask";
		}
		elseif ($press_label == "Ryobi") 
		{
			$sql = "UPDATE task SET cost = (175 * $form_num) WHERE idtask = $task->idtask";
		}
		$query = mysql_query($sql);
	}
	
	/**
	 * CostCutting
	 *	 
     * рассчет стоимости резки
	 *
	 * :WARNING: 
	 *
	 * расчитывается по прайсу "arPriceCutting".
	 *
	 * если формат > A6 и тираж изделия < 20000 - бесплатно, 
	 * если > 20000 - 0.01 за изделие
	 *
	 * если формат < A8 - коэфф. 2
	 *
	 * @param	object	$task	задание на вывод форм
     */
	function CostCutting($task) 
	{
		$cut_temp = explode("\r", $task->value);
		$cut_type = trim(str_replace("тип : ", "", $cut_temp[0]));
		
		if ($cut_type == "в листах") 
		{
			$sql = "UPDATE task SET cost = 0 WHERE idtask = $task->idtask";
			$query = mysql_query($sql);
		} 
		else 
		{
			$sql = "SELECT product_id AS product_id, size AS size, run AS run 
			FROM press_track_new.relation, press_track_new.product
			WHERE task_id = $task->idtask
			AND idproduct = product_id";
			$query = mysql_query($sql);	
			
			$task->cost = 0;
			$a6 = 148 * 105;
			$a8 = 52 * 74;
			
			while ($product = mysql_fetch_object($query)) 
			{ 
				$size_temp = explode("x", $product->size);
				$size_sq = $size_temp[0] * $size_temp[1];
				
				if ($size_sq >= $a6) 
				{
					if ($product->run >= 20000)
					{
						$task->cost = $task->cost + ($product->run * 0.01);	
					}						
				} 
				else 
				{
					if ($product->run > 10000)
					{
						$task->cost += $product->run * 0.03;
					}
					else
					{
						$i = 1;		
						$p0->run 	= 0;
						$p0->cost 	= 0;		
						$p1->run 	= 0;
						$p1->cost 	= 0;
		
						foreach ($this->arPriceCutting as $key => $value) 
						{
							$i = -1;
							foreach ($value as $key2 => $value2)
							{
								if ($value2['run'] >= $product->run)
								{
									$p1->run = $value2['run'];
									$p1->cost = $value2['cost'];
						
									if ($i > -1)
									{
										$p0->run = $value[$i]['run'];
										$p0->cost = $value[$i]['cost'];
									}
									break 2;
								}
								$i++;
							}
						}					
							
						$y1_y2		= $p0->cost - $p1->cost;
						$x2_x1		= $p1->run - $p0->run;
						$x1y2_x2y1	= ($p0->run * $p1->cost) - ($p1->run * $p0->cost);
		
						$f			= ($y1_y2 / $x2_x1) * -1;
						$s			= ($x1y2_x2y1 / $x2_x1) * -1;
		
						$cost		= ($f * $product->run + $s);
										
						if ($size_sq < $a8)
						{
							$cost = $cost * 2;
						}
					
						if ($cost < 100)
						{
							$cost = 100;
						}
						$task->cost += $cost;
					}
				}
			}
			$sql = sprintf("UPDATE task SET cost = %f WHERE idtask = %d", $task->cost, $task->idtask);
			$query = mysql_query($sql);
		}		
	}
	
	/**
	 * CostFolding
	 *	 
     * рассчет стоимости фальцовки
	 *
	 * :WARNING: 
	 *
	 * если тираж изделия: > 3000 - 0.12, > 5000 - 0.105, > 7000 - 0.09, > 10000 - 0.075, < 10000 - 0.066
	 * мин - 210
	 * настройка - 150
	 *
	 * @param	object	$task	задание на вывод форм
     */
	function CostFolding($task) 
	{
		$sql = "SELECT product_id AS product_id, run AS run 
			FROM press_track_new.relation, press_track_new.product
			WHERE task_id = $task->idtask
			AND idproduct = product_id";
		$query = mysql_query($sql);	
			
		$task->cost = 0;
		
		while ($product = mysql_fetch_object($query)) 
		{ 
			if ($product->run < 3000) 
			{
				$cost = $product->run * 0.12;
			} 
			elseif ($product->run < 5000) 
			{
				$cost = $product->run * 0.105;
			} 
			elseif ($product->run < 7000) 
			{
				$cost = $product->run * 0.09;
			} 
			elseif ($product->run < 10000) 
			{
				$cost = $product->run * 0.075;
			} 
			else 
			{
				$cost = $product->run * 0.066;
			}
			
			$task->cost += $cost;
		}
		$task->cost += 150;
		if ($task->cost < 210) $task->cost = 210;
		
		$sql = sprintf("UPDATE task SET cost = %f WHERE idtask = %d", $task->cost, $task->idtask);
		$query = mysql_query($sql);
	}
	
	/**
	 * CostPress
	 *	 
     * рассчет стоимости печати
	 *
	 * :WARNING: 
	 *
	 * расчитывается по прайсам "arPriceSM52" и "arPriceSM74".
	 *
	 * @param	object	$task	задание на вывод форм
     */
	function CostPress($task)
	{
		//$calc = new CalcOrderClass();
		//return $calc->arPriceSM52[1][0]["run"];
		
		$ar_press		= explode("\r", $task->value);
		$press_name		= trim(str_replace("машина : ",			"",	$ar_press[0]));
		
		$press_color	= trim(str_replace("красочность : ",	"", $ar_press[4]));
		$ar_color 		= explode("(", $press_color);
		$ar_color 		= explode("+", $ar_color[0]);
		$press_color	= $ar_color[0] + $ar_color[1];
		
		$press_run		= trim(str_replace("тираж : ",			"", $ar_press[1]));
		$press_run 		= trim(str_replace("c/c", 				"", $press_run));
		$ar_temp 		= explode(",", $press_run);		
		$press_run = 0;
		foreach ($ar_temp as $press_part) 
		{
			$temp = explode("x", $press_part);
			if (count($temp) > 1)
			{
				$press_run = $press_run + ($temp[0] * $temp[1]);
			}
			else
			{
				$press_run = $press_run + $temp[0];
			}
		}
						
		if ($press_name == "SM-52" || $press_name == "Ryobi")
		{
			$price = $this->arPriceSM52;		
		}
		else if ($press_name == "SM-74")
		{
			$price = $this->arPriceSM74;
		}
		
		if ($press_run < $price[1][0]["run"])
		{
			$press_run = $price[1][0]["run"];
		}
				
		$i = 1;		
		$p0->run 	= 0;
		$p0->cost 	= 0;		
		$p1->run 	= 0;
		$p1->cost 	= 0;
		
		foreach ($price as $key => $value) 
		{
			if($key >= $press_color)
			{
				$i = -1;
				foreach ($value as $key2 => $value2)
				{
					if ($value2['run'] >= $press_run)
					{
						$p1->run = $value2['run'];
						$p1->cost = $value2['cost'];
						
						if ($i > -1)
						{
							$p0->run = $value[$i]['run'];
							$p0->cost = $value[$i]['cost'];
						}
						break 2;
					}
					$i++;
				}
			}				
		}
		
		$y1_y2			= $p0->cost - $p1->cost;
		$x2_x1			= $p1->run - $p0->run;
		$x1y2_x2y1 		= ($p0->run * $p1->cost) - ($p1->run * $p0->cost);
		
		$f				= ($y1_y2 / $x2_x1) * -1;
		$s				= ($x1y2_x2y1 / $x2_x1) * -1;
		
		$task->cost 	=  ($f * $press_run + $s) * 30;
		
		$sql = sprintf("UPDATE task SET cost = %f WHERE idtask = %d", $task->cost, $task->idtask);
		$query = mysql_query($sql);
	}
	
	/**
	 * CostВinding
	 *	 
     * рассчет стоимости скрепления
	 *
	 * :WARNING: 
	 *
	 * расчитывается по прайсам "arPriceFoldingTermo" и "arPriceFoldingClip".
	 *
	 * @param	object	$task	задание на вывод форм
     */
	function CostВinding($task) 
	{
		$sql = "SELECT product_id AS product_id, size AS size, run AS run "
			 . "FROM press_track_new.relation, press_track_new.product "
			 . "WHERE task_id = $task->idtask AND idproduct = product_id";
		$query = mysql_query($sql);
		
		$run 	= 0;
		while ($product = mysql_fetch_object($query)) 
		{ 
			$run = $run + $product->run;
		}
			
		$task->cost = 0;
		$i = 1;		
		$p0->run 	= 0;
		$p0->cost 	= 0;		
		$p1->run 	= 0;
		$p1->cost 	= 0;
		
		if (strpos($task->value, "термо") > 0) 
		{
			if ($run < 100) 
			{
				$task->cost = $run * 10;
			} 
			elseif ($run > 10000) 
			{
				$task->cost = $run * 3.5;
			} 
			else
			{
				foreach ($this->arPriceFoldingTermo as $key => $value) 
				{
					$i = -1;
					foreach ($value as $key2 => $value2)
					{
						if ($value2['run'] >= $run)
						{
							$p1->run = $value2['run'];
							$p1->cost = $value2['cost'];
							break 2;
						}
						$i++;
					}
				}
				$task->cost = 600 + ($run * $p1->cost);
			} 			
		} 
		elseif (strpos($task->value, "скреп") > 0)
		{			
			foreach ($this->arPriceFoldingClip as $key => $value) 
			{
				$i = -1;
				foreach ($value as $key2 => $value2)
				{
					if ($value2['run'] >= $run)
					{
						$p1->run = $value2['run'];
						$p1->cost = $value2['cost'];
						
						if ($i > -1)
						{
							$p0->run = $value[$i]['run'];
							$p0->cost = $value[$i]['cost'];
						}
						break 2;
					}
					$i++;
				}
			}					
							
			$y1_y2		= $p0->cost - $p1->cost;
			$x2_x1		= $p1->run - $p0->run;
			$x1y2_x2y1	= ($p0->run * $p1->cost) - ($p1->run * $p0->cost);
		
			$f			= ($y1_y2 / $x2_x1) * -1;
			$s			= ($x1y2_x2y1 / $x2_x1) * -1;
		
			$cost		= ($f * $run + $s);
										
			if ($cost < 1000)
			{
				$cost = 1000;
			}
			$task->cost = $cost;			
		}
		
		$sql = sprintf("UPDATE task SET cost = %f WHERE idtask = %d", $task->cost, $task->idtask);
		$query = mysql_query($sql);
	}
	
	/**
	 * CostPerforation
	 *	 
     * рассчет стоимости перфорации
	 *
	 * :WARNING: 
	 *
	 * расчитывается по прайсам "arPricePerforation".
	 *
	 * @param	object	$task	задание на вывод форм
     */
	function CostPerforation($task)
	{
		$sql = "SELECT product_id AS product_id, size AS size, run AS run "
			 . "FROM press_track_new.relation, press_track_new.product "
			 . "WHERE task_id = $task->idtask AND idproduct = product_id";
		$query = mysql_query($sql);
		
		$run 	= 0;
		while ($product = mysql_fetch_object($query)) 
		{ 
			$run = $run + $product->run;
		}
			
		$task->cost = 150;
		
		if ($run < 3000) 
		{
			$task->cost = $task->cost + ($run * 0.18);
		} 
		elseif ($run > 10000) 
		{
			$task->cost = $task->cost + ($run * 0.09);
		} 
		else
		{
			foreach ($this->arPricePerforation as $key => $value) 
			{
				foreach ($value as $key2 => $value2)
				{
					if ($value2['run'] >= $run)
					{
						$task->cost = $task->cost + ($run * $value2['cost']);
						break 2;
					}
				}
			}			
		} 			
		
		if ($task->cost < 300) 
		{
			$task->cost = 300;
		}
		
		$sql = sprintf("UPDATE task SET cost = %f WHERE idtask = %d", $task->cost, $task->idtask);
		$query = mysql_query($sql);
	}
	
	/*
	laminatorCost - стоимость ламинации
	*/
	function laminatorCost($task) {
		$sql = "SELECT product_id AS product_id FROM press_track_new.relation WHERE task_id = $task->idtask LIMIT 1";
		$query = mysql_query($sql);
		$pr = mysql_fetch_object($query);
		
		$sql = "SELECT value AS value FROM press_track_new.task WHERE press_track_new.task.label = 'печать' 
		AND press_track_new.task.idtask IN ( SELECT task_id FROM press_track_new.relation WHERE product_id = $pr->product_id )";
		$query = mysql_query($sql);
		$press_task = mysql_fetch_object($query);
		$press_temp = explode("\r", $press_task->value);
		$press_label = trim(str_replace("машина : ", "", $press_temp[0]));
		$press_run = trim(str_replace("тираж : ", "", $press_temp[1]));		
		
		$laminator_temp = explode("\r", $task->value);
		$laminator_type = trim(str_replace("тип : ", "", $laminator_temp[0]));
		$num_temp = trim(str_replace("красочность : ", "", $laminator_temp[1]));
		$ar_num_temp = explode("+", $num_temp);
		$laminator_num = $ar_num_temp[0] + $ar_num_temp[1];
		
		$task->cost = 600;
		
		if ($press_label == "SM-52" || $press_label == "Ryobi") {
			if ($laminator_type == "глянцевая") {
				if ($press_run < 501) {
					$cost = $press_run * 2.6;
				} else if ($press_run < 1001) {
					$cost = $press_run * 2.2;
				} else if ($press_run < 2001) {
					$cost = $press_run * 1.65;
				} else if ($press_run < 3001) {
					$cost = $press_run * 1.5;
				} else if ($press_run < 5001) {
					$cost = $press_run * 1.38;
				} else if ($press_run < 10000) {
					$cost = $press_run * 1.33;
				} else {
					$cost = $press_run * 1.3;
				}
			} 
			else if ($laminator_type == "матовая") {
				if ($press_run < 501) {
					$cost = $press_run * 3.25;
				} else if ($press_run < 1001) {
					$cost = $press_run * 2.7;
				} else if ($press_run < 2001) {
					$cost = $press_run * 2.06;
				} else if ($press_run < 3001) {
					$cost = $press_run * 1.88;
				} else if ($press_run < 5001) {
					$cost = $press_run * 1.73;
				} else if ($press_run < 10000) {
					$cost = $press_run * 1.66;
				} else {
					$cost = $press_run * 1.6;
				}			
			}		
		}
		else if ($press_label == "SM-74") {
			if ($laminator_type == "глянцевая") {
				if ($press_run < 501) {
					$cost = $press_run * 4.94;
				} else if ($press_run < 1001) {
					$cost = $press_run * 4.2;
				} else if ($press_run < 2001) {
					$cost = $press_run * 3.14;
				} else if ($press_run < 3001) {
					$cost = $press_run * 2.85;
				} else if ($press_run < 5001) {
					$cost = $press_run * 2.62;
				} else if ($press_run < 10000) {
					$cost = $press_run * 2.53;
				} else {
					$cost = $press_run * 2.5;
				}
			} 
			else if ($laminator_type == "матовая") {
				if ($press_run < 501) {
					$cost = $press_run * 6.5;
				} else if ($press_run < 1001) {
					$cost = $press_run * 5.3;
				} else if ($press_run < 2001) {
					$cost = $press_run * 4.12;
				} else if ($press_run < 3001) {
					$cost = $press_run * 3.76;
				} else if ($press_run < 5001) {
					$cost = $press_run * 3.46;
				} else if ($press_run < 10000) {
					$cost = $press_run * 3.32;
				} else {
					$cost = $press_run * 3.25;
				}			
			}
		}
		
		if ($cost > $task->cost) $task->cost = $cost;
		$task->cost = $task->cost * $laminator_num;
		$sql = sprintf("UPDATE task SET cost = %f WHERE idtask = %d", $task->cost, $task->idtask);
		$query = mysql_query($sql);
	}
	
	/*
	stanzCost - стоимость вырубки
	*/
	function stanzCost($task) {
		$sql = "SELECT product_id AS product_id FROM press_track_new.relation WHERE task_id = $task->idtask";
		$query = mysql_query($sql);
		
		while ($product = mysql_fetch_object($query)) {
			$ArrayOfProduct[] = $product;
			$product_id = $product->product_id;
		}
		
		$sql = "SELECT value AS value FROM press_track_new.task WHERE press_track_new.task.label = 'печать' 
		AND press_track_new.task.idtask IN ( SELECT task_id FROM press_track_new.relation WHERE product_id = $product_id )";
		$query = mysql_query($sql);
		$press_task = mysql_fetch_object($query);
		$press_temp = explode("\r", $press_task->value);
		$press_run = trim(str_replace("тираж : ", "", $press_temp[1]));
		$press_run = trim(str_replace("c/c", "", $press_run));
		$press_temp = explode(",", $press_run);
		$run = 0;
		foreach($press_temp as $press_part) {
			$temp = explode("x", $press_part);
			if (count($temp) > 1)
				$run = $run + ($temp[0] * $temp[1]);
			else
				$run = $run + $temp[0];
		}
		$press_run = $run;
		$task->cost = 300;
		if($press_run < 1001) {
			$cost = $press_run * 2;
		}
		else if($press_run < 1501) {
			$cost = $press_run * 1.85;
		}
		else if($press_run < 2001) {
			$cost = $press_run * 1.75;
		}
		else if($press_run < 2501) {
			$cost = $press_run * 1.5;
		}
		else if($press_run < 3001) {
			$cost = $press_run * 1.35;
		}
		else if($press_run < 3501) {
			$cost = $press_run * 1.2;
		}
		else if($press_run < 4001) {
			$cost = $press_run * 1.1;
		}
		else if($press_run < 5001) {
			$cost = $press_run * 1;
		}
		else if($press_run < 7001) {
			$cost = $press_run * 0.8;
		}
		else if($press_run < 8001) {
			$cost = $press_run * 0.75;
		}
		else if($press_run < 9001) {
			$cost = $press_run * 0.7;
		}
		else if($press_run < 10001) {
			$cost = $press_run * 0.64;
		}
		else {
			$cost = $press_run * 0.6;
		}
		
		$task->cost = $task->cost + $cost;
		if ($task->cost < 600) $task->cost = 600;
		
		$pr_count = count($ArrayOfProduct);
		if ($pr_count <= 8 && $pr_count > 4)
			$task->cost = $task->cost * 1.1;
		else if ($pr_count <= 12 && $pr_count > 8)
			$task->cost = $task->cost * 1.2;
		else if ($pr_count > 12)
			$task->cost = $task->cost * 1.3;
			
		$sql = sprintf("UPDATE task SET cost = %f WHERE idtask = %d", $task->cost, $task->idtask);
		$query = mysql_query($sql);			
	}
			
	
	
	function proofCost($task) 
	{
		
	}
}
?>